//
//  PurchaseItemDetailTableViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/7/26.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class PurchaseItemDetailTableViewController: BaseTableViewController {
    
    private var _source: Array<JSON> = []
    
    var source: Array<JSON>{
        set{
            _source = newValue
            heights = _source.map({ (json) -> CGFloat in
                
                var height: CGFloat = 0.0
                
                let name = json.dictionary?["procorement"]?.rawString() ?? ""
                
                height = name.size(with: UIFont.systemFont(ofSize: 14), maxSize: CGSize(width: UIScreen.main.bounds.size.width / 3.0, height: 1000)).height + 16
                
                let amount = String(((json.dictionary?["estimatedAmount"]?.int) ?? 0) / 100)
                
                height = height > (amount.size(with: UIFont.systemFont(ofSize: 14), maxSize: CGSize(width: UIScreen.main.bounds.size.width / 3.0, height: 1000)).height + 16) ? height : (amount.size(with: UIFont.systemFont(ofSize: 17), maxSize: CGSize(width: UIScreen.main.bounds.size.width / 3.0, height: 1000)).height + 16)
                
                let remark = json.dictionary?["remark"]?.rawString() ?? ""
                
                height = height > (remark.size(with: UIFont.systemFont(ofSize: 14), maxSize: CGSize(width: UIScreen.main.bounds.size.width / 3.0, height: 1000)).height + 16) ? height : (remark.size(with: UIFont.systemFont(ofSize: 17), maxSize: CGSize(width: UIScreen.main.bounds.size.width / 3.0, height: 1000)).height + 16)

                return height
                
            })
            self.tableView.reloadData()
        }
        get{
            return _source
        }
    }
    
    var heights: Array<CGFloat> = []
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(UINib.init(nibName: "PurchaseDetailTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "PurchaseDetailTableViewCell")
        self.tableView.reloadData()
        
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCell(withIdentifier: "PurchaseDetailTableViewCell")!
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let detailcell = cell as? PurchaseDetailTableViewCell
        
        detailcell?.model = self.source[indexPath.row]
        
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 54
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let header = UIView()
        
        header.backgroundColor = UIColor.hex("eeeeee")
        
        let nameLabel = UILabel()
        
        nameLabel.backgroundColor = .white
        
        nameLabel.font = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.bold)
        
        nameLabel.text = "名称"
        
        nameLabel.textAlignment = .center
        
        nameLabel.frame = CGRect(origin: CGPoint(x: 0, y: 10), size: CGSize(width: self.view.width / 3.0, height: 43.5))
        
        header.addSubview(nameLabel)
        
        let predicateLabel = UILabel()
        
        predicateLabel.backgroundColor = .white
        
        predicateLabel.font = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.bold)
        
        predicateLabel.text = "预算金额(元)"
        
        predicateLabel.textAlignment = .center
        
        predicateLabel.frame = CGRect(origin: CGPoint.init(x: self.view.width / 3.0 + 0.5, y: 10), size: CGSize(width: self.view.width / 3.0, height: 43.5))
        
        header.addSubview(predicateLabel)
        
        let remarkLabel = UILabel()
        
        remarkLabel.backgroundColor = .white
        
        remarkLabel.font = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.bold)
        
        remarkLabel.text = "备注"
        
        remarkLabel.textAlignment = .center
        
        remarkLabel.frame = CGRect(origin: CGPoint.init(x: 2 * self.view.width / 3.0 + 1, y: 10), size: CGSize(width: self.view.width / 3.0, height: 43.5))
        
        header.addSubview(remarkLabel)
        
        return header
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.source.count
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return heights[indexPath.row]
    }

    
    

}
